package com.ys.fmms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.xiaoymin.knife4j.core.util.StrUtil;
import com.ys.fmms.mapper.DataSyncMapper;
import com.ys.fmms.pojo.DataSync;
import com.ys.fmms.pojo.DateTimeVo;
import com.ys.fmms.service.DataSyncService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service(value = "dataSyncServiceImpl")
@Transactional
public class DataSyncServiceImpl extends ServiceImpl<DataSyncMapper, DataSync> implements DataSyncService {
    @Override
    public IPage<DataSync> getDataSyncInfo(Page<DataSync> page, String type, String userName, DateTimeVo date) {
        QueryWrapper<DataSync> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(userName)) {
            wrapper.like("user_name", userName);
        }
        if (StringUtils.hasText(type)) {
            wrapper.like("type", type);
        }
        if (date != null) {
            // 这种有sql注入的风险
            // wrapper.apply(StrUtil.isNotBlank(date.getStartDate()),"DATE_FORMAT(create_time,'%Y-%m-%d')='"+date.getStartDate()+"'");
            // wrapper.or();
            // wrapper.apply(StrUtil.isNotBlank(date.getEndDate()),"DATE_FORMAT(create_time,'%Y-%m-%d')='"+date.getEndDate()+"'");
            wrapper.apply(StrUtil.isNotBlank(date.getStartDate()), "Date_format({0},'%Y-%m-%d')", date.getStartDate());
            wrapper.or();
            wrapper.apply(StrUtil.isNotBlank(date.getEndDate()), "Date_format({0},'%Y-%m-%d')", date.getEndDate());
        }

        IPage<DataSync> iPage = baseMapper.selectPage(page, wrapper);
        return iPage;
    }
}
